package com.bocai.yuweiwangluo.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 修改密码界面
 */
public class SetNewPsdActivity extends BaseActivity implements View.OnClickListener{
    @ViewInject(R.id.et_oldpsd)
    EditText etoldPsd;
    @ViewInject(R.id.et_newpsd)
    EditText etnewPsd;
    @ViewInject(R.id.et_newpsdtrue)
    EditText etNewPsdTrue;
    @ViewInject(R.id.btn_setpsd)
    Button btnSetPsd;
    private BaseBean bean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    @Override
    protected int contentView() {
        return R.layout.activity_set_newpsd;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        initToolbar("修改密码");
        btnSetPsd.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        changePwd();
    }
    public void changePwd(){
        if("".equals(etoldPsd.getText().toString())){
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
        }

        else  if("".equals(etnewPsd.getText().toString()))
        {
            Toast.makeText(this,"请输入新密码",Toast.LENGTH_SHORT).show();
        }
        if(etoldPsd.getText().length()<6){
            Toast.makeText(this, "您设置的密码不足6位", Toast.LENGTH_SHORT).show();
        }
        else if(!etnewPsd.getText().toString().equals(etNewPsdTrue.getText().toString()))
        {
            Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
        }else{
            showLoading();
            MyOkHttpClient myOkHttpClient=new MyOkHttpClient(this,this);
            SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
            String phone=sharedPreferences.getString("user_name","");
            myOkHttpClient.changePsd(UrlData.BASEURL + UrlData.CHANGEPSD, phone,etnewPsd.getText().toString(),etoldPsd.getText().toString(), handler);
        }
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            switch (msg.what)
            {
                case RESPONSE_SUCCEED:
                    Log.e("tag", result);
                    bean = gson.fromJson(result, BaseBean.class);
                        //设置新密码
                        if (0 == bean.getState())
                        {
                            SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("password",etnewPsd.getText().toString());
                            editor.commit();
                            Intent intent=new Intent(SetNewPsdActivity.this, SetNewPsdSuccessActivity.class);
                            startActivity(intent);
                            SetNewPsdActivity.this.finish();
                        } else
                        {
                            Toast.makeText(SetNewPsdActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    break;
                case RESPONSE_FAILED:
                    showError(result);
                    break;

            }
        }
    };

}
